//
//  UIApplication+ZXOpenURL.m
//  CAICategories
//
//  Created by simon on 2021/8/26.
//

#import "UIApplication+ZXOpenURL.h"

@implementation UIApplication (ZXOpenURL)



+ (void)zx_openURLToCallIphoneWithTel:(NSString *)phone
{
    NSString *allString = [NSString stringWithFormat:@"tel:%@",phone];
    NSURL *url =[NSURL URLWithString:allString];
    if ([[UIApplication sharedApplication] canOpenURL:url])
    {
        [[UIApplication sharedApplication]openURL:url options:@{} completionHandler:nil];
    }
}

+ (void)zx_openURLToSendSMSWithTel:(NSString *)phone
{
    NSURL *relativeURL= [NSURL URLWithString:@"sms:"];
    NSURL *url = [NSURL URLWithString:phone relativeToURL:relativeURL];
    if ([[UIApplication sharedApplication] canOpenURL:url])
    {
        [[UIApplication sharedApplication]openURL:url options:@{} completionHandler:nil];
    }
}

+ (void)zx_openURLToSendMailWithTel:(NSString *)phone
{
    NSURL *relativeURL= [NSURL URLWithString:@"mailto:"];
    NSURL *url = [NSURL URLWithString:phone relativeToURL:relativeURL];
    if ([[UIApplication sharedApplication] canOpenURL:url])
    {
        [[UIApplication sharedApplication]openURL:url options:@{} completionHandler:nil];
    }
}

//  如果指定的URL scheme由另一个app应用程序处理，options可以使用通用链接的key。空的options字典与旧的openURL调用是相同的；
//  当openURL:options:completionHandler:方法的选项字典中有这个key时，如果设置为YES, 则URL必须是通用链接，并且有一个已安装的应用程序被用于打开该URL时，当前app才会打开URL。
//  如果没有其它app应用程序配置这个通用链接，或者用户设置NO禁用打开链接，则completion handler 回调里的success为false(NO);
+ (void)zx_openURLToAppSetting
{
    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if ([[UIApplication sharedApplication] canOpenURL:url])
    {
        [[UIApplication sharedApplication]openURL:url options:@{} completionHandler:nil];
    }
}

//  如果指定的URL scheme由另一个app应用程序处理，options可以使用通用链接的key（UIApplicationOpenURLOptionUniversalLinksOnly:@(YES)）。空的options字典与旧的openURL调用是相同的；
//  当有这个key，如果设置为YES, 则URL必须是通用链接，并且有一个已安装的应用程序被用于打开该URL时，当前app才会打开URL。
//  如果没有其它app应用程序配置这个通用链接，或者用户设置NO禁用打开链接，则completion handler 回调里的success为false(NO)；
+ (void)zx_openURLToItunesForApplicationIdentifier:(NSString *)appId
{
    static NSString *itunesLink = @"http://itunes.apple.com/cn/app/id";
    NSURL *url =[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",itunesLink,appId]];
    if ([[UIApplication sharedApplication] canOpenURL:url])
    {
        [[UIApplication sharedApplication]openURL:url options:@{} completionHandler:nil];
    }
}

+ (void)zx_openURLToAppleMapsWithSearchQuery:(NSString *)query
{
    NSString *urlString = [NSString stringWithFormat:@"http://maps.apple.com/?q=%@",query];
    if (@available(iOS 9.0,*)) {
        urlString = [ urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]];
    }
    NSURL *url =[NSURL URLWithString:urlString];
    if ([[UIApplication sharedApplication] canOpenURL:url])
    {
        [[UIApplication sharedApplication]openURL:url options:@{} completionHandler:nil];
    }
}

+ (void)zx_openURLToQQClientWithQQ:(NSString *)qq canNotOpen:(void(^)(NSString *title))failure
{
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"mqq://"]])
    {
        NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:@"mqq://im/chat?chat_type=wpa&uin=%@&version=1&src_type=web",qq]];
        [[UIApplication sharedApplication]openURL:url options:@{} completionHandler:nil];
      
    }else{
        if (failure) {
            failure(@"请先安装手机QQ哦～");
        }
    }
}

@end
